package cn.huobi.pet.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.LoginContext;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.service.EmployeeService;
import cn.huobi.pet.domain.SearchMasterMsg;
import cn.huobi.pet.dto.AcceptOrderDto;
import cn.huobi.pet.query.SearchMasterMsgQuery;
import cn.huobi.pet.service.ISearchMasterMsgService;
import cn.huobi.user.domain.Logininfo;
import cn.huobi.user.domain.User;
import cn.huobi.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 寻主信息发布
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        Logininfo loginContext = LoginContext.getLoginContext(request);
        User user = userService.findByLoginId(loginContext.getId());
        searchMasterMsg.setUserId(user.getId());

        return searchMasterMsgService.publish(searchMasterMsg);
    }

    /**
     * 查询待处理寻主消息
     *  state=1 && shopId = 当前登录人shopId
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> tohandle(@RequestBody SearchMasterMsgQuery searchMasterMsg,HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(employee.getShopId());

        return searchMasterMsgService.seleteSearchMasterMsg(searchMasterMsg);
    }

    /**
     * 查询待审核的寻主消息
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        searchMasterMsg.setState(0);

        return searchMasterMsgService.seleteSearchMasterMsg(searchMasterMsg);
    }

    /**
     * 查询寻主池中的寻主消息
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        searchMasterMsg.setState(1);
        return searchMasterMsgService.seleteSearchMasterMsg(searchMasterMsg);
    }

    /**
     * 查询已完成的寻主消息
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsg,HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        if(employee.getShopId() != null){
            searchMasterMsg.setShopId(employee.getShopId());
        }
        searchMasterMsg.setState(3);

        return searchMasterMsgService.seleteSearchMasterMsg(searchMasterMsg);
    }

    /**
     *  用户个人中心查询寻主消息
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        return searchMasterMsgService.seleteSearchMasterMsg(searchMasterMsg);
    }

    /**
     * 接收订单
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptOrderDto acceptOrderDto) {
        searchMasterMsgService.accept(acceptOrderDto);
        return AjaxResult.me().success();
    }


    /**
     * 拒绝定单
     */
//    @PostMapping("/reject/{msgId}")
//    public AjaxResult reject(@PathVariable("msgId") Long msgId,HttpServletRequest request) {
//        Logininfo logininfo = LoginContext.getLoginContext(request);
//        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
//        searchMasterMsgService.reject(msgId,employee);
//        return AjaxResult.me().success();
//    }

    /**
     * 拒绝定单
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody AcceptOrderDto acceptOrderDto,HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        searchMasterMsgService.reject(acceptOrderDto,employee);
        return AjaxResult.me().success();
    }
}
